<template>
  <div class="good_detail">
    <div class="header">
        <i class="iconfont icon-fanhui" @click="back()"></i>
      <h3>购物车</h3>
    </div>
    <div class="connect_img">
      <img :src="goodMessage.goods_big_logo?goodMessage.goods_big_logo:'http://image2.suning.cn/uimg/b2c/newcatentries/0070078057-000000000634917020_1_800x800.jpg'">
    </div>
    <div class="connect_price">
      <span>￥{{goodMessage.goods_price}}</span>
    </div>
    <div class="connect_title">
      <span>{{goodMessage.goods_name}}</span>
    </div>
    <div class="bottomer">
      <div class="bt_left">
        <span class="iconfont icon-kefu"></span><br>
        <span>客服</span>
      </div>
      <div class="bt_left">
        <span class="iconfont icon-fenxiang"></span><br>
        <span>分享</span>
      </div>
      <div class="bt_left">
        <router-link to="/Car" @click.native="refresh">
          <span class="iconfont icon-gouwuche1"></span><br>
          <span>购物车</span>
        </router-link>
      </div>
      <div class="bt_right cur" @click="addCar()">
        <span>加入购物车</span>
      </div>
      <div class="bt_right buy">
        <span>立即购买</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      index: this.$route.params.id,
      goodMessage:{},
    }
  },
  created() {
    this.getMessage()
  },
  methods: {
    refresh:function(){
      var isActive = JSON.parse(localStorage.getItem("isActive"));
      for(let i=0;i<isActive.length;i+=1){
        isActive[i]=false
      }
      isActive[2]=true
       localStorage.setItem("isActive", JSON.stringify(isActive));
      this.$router.go(0)
    },
    getMessage(){
      this.axios.get("https://api-hmugo-web.itheima.net/api/public/v1/goods/search")
      .then(res=>{
        this.goodMessage=res.data.message.goods[this.index]
      })
    },
    back(){
      this.$router.go(-1);
    },
    addCar(){
      //获取本地数组数据
      var goods=JSON.parse(localStorage.getItem('goods'))||[]
      let index=goods.findIndex(v=>v.goods_id===this.goodMessage.goods_id)
      if(index==-1){
        //购物车里没有此商品
        this.goodMessage.num=1
        this.goodMessage.checked=true
        goods.push(this.goodMessage)
      }else{
        //购物车里面有此商品
        goods[index].num++
      }
      //把数据重新存储到本地存储中
      localStorage.setItem('goods', JSON.stringify(goods));
      //成功后提示
      this.$message({
          showClose: true,
          message: '添加成功',
          type: 'success',
          duration:500
        });
    },
  },
}
</script>

<style scoped>
*{
  width: 100%;
}
.header{
  width: 100%;
  height: 80px;
  background-color: rgb(226, 73, 73);
  line-height: 80px;
  text-align: center;
  font-size: 16px;
}
.header i{
  float: left;
  width: 10%;
}

.connect_price{
  width: 95%;
  color:red;
  padding-left: 5%;
  font-size: 16px;
}
.connect_title{
  width: 95%;
  font-size: 16px;
  padding: 5px 0;
  padding-left: 5%;
  border-top: 1px solid rgb(211, 205, 205);
  border-bottom: 1px solid rgb(211, 205, 205);
}
.bottomer{
  width: 97%;
  height: 60px;
  position: fixed;
  bottom: 0;
  display: flex;
  line-height: 30px;
  background-color: white;
  padding-left: 3%;
  font-size: 14px;
}
.bottomer .bt_left{
  text-align: center;
  flex:1;
}
.bottomer .bt_right{
  flex:2;
  text-align: center;
  line-height: 60px;
  color: white;
  font-size: 18px;
}
.cur{
  background-color: rgb(240, 206, 54);
}
.buy{
  background-color: rgb(238, 94, 94);
}
.connect_img{
  height: 300px;
}
.connect_img img{
  width: 100%;
  height: 100%;
}
</style>